/* eslint-disable eol-last */
/* eslint-disable space-before-function-paren */

import qs from 'qs'
const apiMethods = {
    methods: {
        getRequest(url, data) {
            return new Promise((resolve, reject) => {
                window.axios.get(url, data).then((response) => {
                    resolve(response.data)
                }, (response) => {
                    reject(response)
                    window._g.closeGlobalLoading()
                    console.log(response)
                    this.handleError(response)
                })
            })
        },
        postRequest(url, data) {
            return new Promise((resolve, reject) => {
                window.axios.post(url, qs.stringify(data)).then((response) => {
                    resolve(response.data)
                }).catch((response) => {
                    console.log('f', response)
                    resolve(response)
                    window.bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        },
        deleteRequest(url, id) {
            return new Promise((resolve, reject) => {
                window.axios.delete(url + id).then((response) => {
                    resolve(response.data)
                }, (response) => {
                    reject(response)
                    window._g.closeGlobalLoading()
                    window.bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        },
        putRequest(url, id, obj) {
            return new Promise((resolve, reject) => {
                window.axios.put(url + id, obj).then((response) => {
                    resolve(response.data)
                }, (response) => {
                    window._g.closeGlobalLoading()
                    window.bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                    reject(response)
                })
            })
        },
        uploadRequest(url, data) {
            return new Promise((resolve, reject) => {
                window.axios.post(url, data, {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                }).then((response) => {
                    resolve(response.data)
                }).catch((response) => {
                    console.log('f', response)
                    resolve(response)
                    window.bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        },
        reAjax(url, data) {
            return new Promise((resolve, reject) => {
                window.axios.post(url, data).then((response) => {
                    resolve(response.data)
                }, (response) => {
                    reject(response)
                    window.bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        }
    }
}

export default apiMethods